/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-15 19:16:43
 * Last Modified: 2017-07-22 13:10:56
 * File Name: account/accountinfo.go
 * Description:
 */
package account

import (
	"time"

	"passport/xfile"
)

/*
	账号信息的修改器
*/
type AccountInfoChanger struct {
	account     string
	accountType string
	newData     map[string]interface{}
}

func NewAccountInfoChanger(account, accountType string) *AccountInfoChanger {
	return &AccountInfoChanger{
		account:     account,
		accountType: accountType,
		newData:     make(map[string]interface{}),
	}
}

/*
	将修改器中的信息保存到redis中去
*/
func (p *AccountInfoChanger) Save() error {
	if len(p.newData) == 0 { //无更新
		return nil
	}

	p.newData["update_time"] = time.Now().Format("2006-01-02 15:04:05")
	err := GetAccountService().setAnyAccountInfoByMap(p.accountType, p.account, p.newData)
	if err == nil {
		item := &xfile.PassportInfoItem{}
		item.TableName = xfile.PI_ACCOUNT
		if _, ok := p.newData["account"]; ok {
			item.Op = xfile.OP_INSERT
		} else {
			item.Op = xfile.OP_UPDATE
			p.SetAccount(p.account)
			p.SetAccountType(p.accountType)
		}
		item.Data = p.newData
		xfile.GetPassportInfoMgr().SavePassportInfo(item)
	}
	return err
}

/*
	删除账号信息
*/
func (p *AccountInfoChanger) Delete() error {
	err := GetAccountService().deleteAccountInfo(p.accountType, p.account)
	if err == nil {
		p.newData["update_time"] = time.Now().Format("2006-01-02 15:04:05")
		p.SetAccount(p.account)
		p.SetAccountType(p.accountType)
		item := &xfile.PassportInfoItem{}
		item.TableName = xfile.PI_ACCOUNT
		item.Op = xfile.OP_DELETE
		item.Data = p.newData
		xfile.GetPassportInfoMgr().SavePassportInfo(item)
	}
	return err
}

/*
	设置账号ID
*/
func (p *AccountInfoChanger) SetAccount(account string) {
	p.newData["account"] = account
}

/*
	设置账号类型
*/
func (p *AccountInfoChanger) SetAccountType(accountType string) {
	p.newData["account_type"] = accountType
}

/*
	设置昵称
*/
func (p *AccountInfoChanger) SetNickName(nickName string) {
	if len(nickName) <= 32 {
		p.newData["nick_name"] = nickName
	} else {
		p.newData["nick_name"] = nickName[:32]
	}
}

/*
	设置激活状态
*/
func (p *AccountInfoChanger) SetActive(active int8) {
	p.newData["active"] = active
}

/*
	设置是否第三方账号
*/
func (p *AccountInfoChanger) SetThirdAccount(thirdAccount int8) {
	p.newData["third_account"] = thirdAccount
}

/*
	设置绑定状态
*/
func (p *AccountInfoChanger) SetBindStatus(bindstatus int8) {
	p.newData["bind_status"] = bindstatus
}

/*
	设置关联的用户ID
*/
func (p *AccountInfoChanger) SetUserid(userid string) {
	p.newData["userid"] = userid
}

/*
	设置插入时间
*/
func (p *AccountInfoChanger) SetInsertTime(insertTime string) {
	p.newData["insert_time"] = insertTime
}
